package com.card.admin.servelt;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.card.notice.GlobNotice;
import com.card.notice.NoticeManager;

public class AddNoticeServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		res.setContentType("text/plain;charset=utf-8");
		req.setCharacterEncoding("utf-8");
		
		String url="";
		
		String action =req.getParameter("action");
		if(action.equals("add"))
		{
			String description = req.getParameter("des");
			String starttime = req.getParameter("starttime");
			String endtime = req.getParameter("endtime");
			
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			Date startdate = null;
			Date enddate = null;
			try {
				startdate=sdf.parse(starttime);
				enddate = sdf.parse(endtime);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			GlobNotice notice = new GlobNotice(description, startdate, enddate);
			NoticeManager.getInstance().addNewNotice(notice);
			
		}else if(action.equals("delete"))
		{
			String id = req.getParameter("ID");
			NoticeManager.getInstance().removeNoticeFromDB(id);
		}
		
		req.getRequestDispatcher("/NoticeShow.jsp").forward(req, res);
	}
}
